package example.documentbased.mainModule.utils
{
	import example.documentbased.mainModule.models.vo.DocumentVO;
	
	import mx.collections.ArrayList;

	public class ItemUtils
	{
		public static function createUniqueName(documents:ArrayList, name:String):String
		{
			if (!(documents || documents.length))
				return name;
			
			var newName:String = name;
			var i:int = 2;
			
			while(containsItemWithName(documents, newName))
			{
				newName = name + " (" + i + ")";
				i++;
			}
			
			return newName;
		}
		
		private static function containsItemWithName(documents:ArrayList, name:String):Boolean
		{
			const arr:Array = documents.source;
			
			for (var i:int = 0; i < arr.length; i++) 
			{
				if (DocumentVO(arr[i]).name == name)
					return true;
			}
			
			return false;
		}
	}
}